package gipfProject.games.zertz.gui;

import gipfProject.tools.ResourceLoader;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JPanel;

import gipfProject.games.zertz.data.PieceData;
import gipfProject.games.zertz.data.ZertzData;

public class SelectMarbleUI extends JPanel implements MouseListener {
	ZertzData gameState;
	ResourceLoader images;
	static Color backgroundColor;

	SelectMarbleUI(ResourceLoader images, ZertzData gameState) {
		this.gameState = gameState;
		this.images = images;
		backgroundColor = Color.blue;

		addMouseListener(this);
	}


	public void paintComponent(Graphics g) {

		//Erase the previously-drawn graphics
		g.setColor(Color.blue );
		g.fillRect(10, 10, getSize().width-21,getSize().height-21);

		//Draw a border around the board
		g.setColor(Color.black);
		g.drawRect(10,10,getSize().width-21,getSize().height-21);
		g.drawRect(11,11,getSize().width-23,getSize().height-23);

		//Draw the name box
		g.setColor( Color.cyan );
		g.fillRect(85, 0, 150, 18);

		//Paint a border on it
		g.setColor( Color.black );
		g.drawRect(85, 0, 149, 18);

		//And write the caption
		g.setColor( Color.black );
		g.drawString("Select marble to place:",90,13);

		drawSelectionArea(g);
		drawPool(g);
	}
	
	private void drawSelectionArea(Graphics g){
		//Draw one of each ring for selection purposes
		g.drawImage(images.getResource("WhiteRing"), 30, 30, this);
		g.drawImage(images.getResource("GrayRing"),  30, 70, this);
		g.drawImage(images.getResource("BlackRing"), 30,110, this);

		//Print the quantity
		g.setFont(new Font("Helvetica", Font.BOLD,  16));
		g.setColor(Color.white);
		g.drawString("" + gameState.getPool(PieceData.WHITE),70, 52);
		g.drawString("" + gameState.getPool(PieceData.GRAY),70, 92);
		g.drawString("" + gameState.getPool(PieceData.BLACK),70,132);
		
		//Print the number of marbles in that pool
		g.setFont(new Font("Helvetica", Font.BOLD,  16));
		g.setColor(Color.white);

		//Highlight the selected ring, if any
		if (gameState.getPlaceType() >= 0 && gameState.getPlaceType() < 3){
			int Size = 30;
			int yOffset = (gameState.getPlaceType()) * 40;
			g.setColor( Color.yellow );
			g.drawRect(30,  30+yOffset,  Size+1,Size+1);
			g.drawRect(30-1,30+yOffset-1,Size+3,Size+3);
		}
	}
	
	private String getPieceFile(int pieceColor){
		if (pieceColor == PieceData.WHITE) {
			return "WhiteMarble";
		} else if (pieceColor == PieceData.GRAY) {
			return "GrayMarble";
		} else if (pieceColor == PieceData.BLACK) {
			return "BlackMarble";
		} else {
			return null;
		}
	}
	
	private void drawPool(Graphics g){
		
		//Draw one of each ring for selection purposes
		
		for (int pieceColor = 0 ; pieceColor < 3 ; pieceColor++){
			for (int i = 0; i < gameState.getPoolSize(pieceColor); i++){
				if (i < gameState.getPool(pieceColor)){
					g.drawImage(images.getResource(getPieceFile(pieceColor)), 100+(20*i), 39+(40*pieceColor), this);
				}else{
					g.drawImage(images.getResource("EmptyMarble"), 100+(20*i), 39+(40*pieceColor), this);
				}
			}
		}
	}

	public void mousePressed(MouseEvent e) {
		int col = (e.getX()-24) / 40;
		int row = (e.getY()-24) / 40;

		//If the result is within the bounds of the board, process the click
		if (row >= 0 && row < 3 && col == 0)
		{
			gameState.setPlaceType(row);
		}

		repaint();
	}

	//These are not used
	public void mouseClicked(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}

}//END OF CLASS


